/*
 * Copyright (c) 2004-2006 The Regents of The University of Michigan
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are
 * met: redistributions of source code must retain the above copyright
 * notice, this list of conditions and the following disclaimer;
 * redistributions in binary form must reproduce the above copyright
 * notice, this list of conditions and the following disclaimer in the
 * documentation and/or other materials provided with the distribution;
 * neither the name of the copyright holders nor the names of its
 * contributors may be used to endorse or promote products derived from
 * this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *
 * Authors: Gou Pengfei
 *
 * Date:    May 2011
 */

#ifndef __CPU_EDGE_PREDICTOR_TABLE_HH__
#define __CPU_EDGE_PREDICTOR_TABLE_HH_

#include <vector>

#include "base/hashmap.hh"
#include "base/types.hh"
#include "base/trace.hh"

using namespace std;

template<class Impl> class BasePredictor;

template<class Data, class Impl>
class PredictorTable : public vector<Data>
{
  public:

    typedef typename m5::hash_map<Addr, Data*>::iterator TableIt;

    PredictorTable(BasePredictor<Impl> * ptr,
            bool is_alias_free = false)
        : std::vector<Data>(),
        isAliasFree(is_alias_free),
        predictorPtr(ptr)
    {
        table.clear();
    }

    ~PredictorTable()
    {
        if (isAliasFree) {
            TableIt it = table.begin();

            while (it != table.end()) {

                delete it->second;

                it++;
            }

            table.clear();
        }
    }

    std::string name() const 
    {
        return "PredictorTable";
    }

  private:

    m5::hash_map<Addr, Data*> table;

    bool isAliasFree;

    BasePredictor<Impl> * predictorPtr;

  public:

    void clear() 
    {
        if (isAliasFree) {
            table.clear();
        }

        std::vector<Data>::clear();
    }

    Data & operator[] (Addr idx)
    {
        if (isAliasFree) {
            TableIt it = table.find(idx);

            if (it == table.end()) {
                Data * entry = new Data(predictorPtr);
                table.insert(std::make_pair(idx, entry));

                DPRINTF(PredictorTable, "Allocate new entry for %#x\n", idx);

                return *entry;
            } else {

                DPRINTF(PredictorTable, "Find entry for %#x\n", idx);

                return *(it->second);
            }
        } else {
            DPRINTF(PredictorTable, "Access vector.\n");
            return vector<Data>::operator[](idx);
        }
    }

    const Data & operator[] (Addr idx) const
    {
        if (isAliasFree) {
            TableIt it = table.find(idx);

            if (it == table.end()) {
                Data * entry = new Data(predictorPtr);
                table.insert(std::make_pair(idx, entry));

                DPRINTF(PredictorTable, "Allocate new entry for %#x\n", idx);

                return *entry;
            } else {

                DPRINTF(PredictorTable, "Find entry for %#x\n", idx);

                return *(it->second);
            }
        } else {
            DPRINTF(PredictorTable, "Access vector.\n");
            return vector<Data>::operator[](idx);
        }
    }

};

#endif //__CPU_EDGE_PREDICTOR_TABLE_HH__
